package com.ywjh.inter.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.entity.BindInsulinEntity;
import com.ywjh.inter.entity.BloodGlucoseEntity;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.entity.UsersHXEntity;
import com.ywjh.inter.utils.PinyinUtils;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DoctorService {

	private static DoctorService instance;

	public static DoctorService getInstance() {
		if (instance == null) {
			instance = new DoctorService();
		}
		return instance;
	}

	public Map<Integer, List<JSONObject>> listPatientGroupByDisId(int doc_id) throws IOException {
		Map<Integer, List<JSONObject>> map = new HashMap<>();
		System.out.println("DocService.getInstance()=="+DocService.getInstance());
		System.out.println("DocService.getInstance().getDisease=="+DocService.getInstance().getDisease());
		for(int i=0;i<DocService.getInstance().getDisease().size();i++) {
			JSONObject json=DocService.getInstance().getDisease().getJSONObject(i);
			map.put(json.getInt("id"), new LinkedList<JSONObject>());
		}
		JSONObject resultJson = HXService.getInstance().getFriendList("user_" + doc_id,
				HXService.getInstance().token());
		JSONArray friendArray = resultJson.getJSONArray("data");
		for (int i = 0; i < friendArray.size(); i++) {
			String hx_username = friendArray.getString(i);
			int friendID = Integer
					.parseInt(hx_username.substring(hx_username.lastIndexOf("_") + 1, hx_username.length()));
			UsersEntity u = UsersService.getInstance().findUserById(friendID);
			if (u != null) {
				if (u.getIs_doc() == 0) {
					ProfileEntity profileEntity = UsersService.getInstance().getProfileByUserId(friendID);
					JSONObject patientJson = new JSONObject();
					patientJson.put("head_url", SystemConfig.SERVER_DOMAIN + u.getHead_url());
					patientJson.put("loginname", u.getLoginname());
					patientJson.put("phone", u.getPhone());
					patientJson.put("hx_username", hx_username);
					patientJson.put("initial", "#");
					patientJson.put("is_doc", 0);
					patientJson.put("is_check_today", PatientDataService.getInstance().is_check_today(u.getId()));
					patientJson.put("is_7d_no_check", PatientDataService.getInstance().is_7d_no_check(u.getId()));
					patientJson.put("is_7d_alarm", PatientDataService.getInstance().is_7d_alarm(u.getId()));
					int diseaseId = 0;
					if (profileEntity != null) {
						diseaseId=profileEntity.getDisease_id();
						
						String py = PinyinUtils.toPinyinString(profileEntity.getName());
						if (py != null && py.length() > 1) {
							patientJson.put("initial", py.substring(0, 1).toUpperCase());
						}
						BindInsulinEntity insulinEntity=InsulinService.getInstance().findInsulinByPatientId(u.getId());
						JSONObject profileJson = JSONObject.fromObject(profileEntity.toJson(u, insulinEntity));
						patientJson.put("profile", profileJson);
						BloodGlucoseEntity bloodGlucoseEntity = PatientDataService.getInstance()
								.findBloodBluLeastByUserId(profileEntity.getUser_id());
						if (bloodGlucoseEntity != null) {
							JSONObject bglu = new JSONObject();
							bglu.put("data", bloodGlucoseEntity.getData());
							bglu.put("check_time", StrUtil.getYYYYMMDDHHMISS(bloodGlucoseEntity.getCheck_time()));
							bglu.put("plan_id", bloodGlucoseEntity.getPlan_id());
							bglu.put("alarm", PatientDataService.getInstance().getAlarm(bloodGlucoseEntity.getUser_id(),
									bloodGlucoseEntity.getPlan_id(), bloodGlucoseEntity.getData()));
							patientJson.put("least_bglu", bglu);
						}
					}
					if (map.get(diseaseId) != null) {
						List<JSONObject> list = map.get(diseaseId);
						list.add(patientJson);
						map.put(diseaseId, list);
					} else {
						List<JSONObject> list = new LinkedList<JSONObject>();
						list.add(patientJson);
						map.put(diseaseId, list);
					}
				}
			}
		}
		return map;
	}

	public List<Integer> getPatientIdList(int docId) {
		UsersHXEntity doc = UsersService.getInstance().findHXUserByUserId(docId);
		List<Integer> list = new LinkedList<>();

		JSONObject resultJson;
		try {
			resultJson = HXService.getInstance().getFriendList(doc.getUsername(), HXService.getInstance().token());
			JSONArray friendArray = resultJson.getJSONArray("data");
			for (int i = 0; i < friendArray.size(); i++) {
				String hx_username = friendArray.getString(i);
				int friendID = Integer
						.parseInt(hx_username.substring(hx_username.lastIndexOf("_") + 1, hx_username.length()));
				UsersEntity friend = UsersService.getInstance().findUserById(friendID);
				if (friend != null && friend.getIs_doc() == 0) {
					list.add(friendID);
				}
			}
		} catch (IOException e) {
			System.out.println("hx getFriendList 接口错误");
			e.printStackTrace();
		}
		return list;
	}
}
